package model

import (
	"time"

	"github.com/pkg/errors"

	"bilinadmin/pkg/util"
)

const (
	// BrandBizTypeSelfRun 自营模式
	BrandBizTypeSelfRun int8 = 0
	// BrandBizTypePlatform 品牌模式
	BrandBizTypePlatform int8 = 1
	// BrandBizTypeAgent 经销商模式
	BrandBizTypeAgent int8 = 2

	BrandBizTypeSelfRunStr  string = "self"
	BrandBizTypePlatformStr string = "platform"
	BrandBizTypeAgentStr    string = "agent"

	// BrandStatusOK 正常
	BrandStatusOK int8 = 1
	// BrandStatusBlocked 停用
	BrandStatusBlocked int8 = 2
)

var InvalidType = errors.New("invalid type enum")

// AppScope 应用开通情况
type AppScope struct {
	Bg         bool      `json:"bg"`           // 比邻星团
	BgOpTime   time.Time `json:"bg_op_time"`   // 操作时间
	BgOpEmp    int64     `json:"bg_op_emp"`    // 操作人
	Mall       bool      `json:"mall"`         // 比邻商城
	MallOpTime time.Time `json:"mall_op_time"` // 操作时间
	MallOpEmp  int64     `json:"mall_op_emp"`  // 操作人
}

// Brand 品牌
type Brand struct {
	Id          int64  `json:"id" sql:"id"`                   // 品牌id
	Name        string `json:"name" sql:"name"`               // 品牌名
	Logo        string `json:"logo" sql:"logo"`               // 品牌logo
	Description string `json:"description" sql:"description"` // 品牌描述
	SuEmpId     int64  `json:"su_emp_id" sql:"su_emp_id"`     // 超级管理员id
	BizType     int8   `json:"biz_type" sql:"biz_type"`       // 经营模式 1自营 2平台 3经销商
	Status      int8   `json:"status" sql:"status"`           // 状态 1正常 2停用 3删除
	CreatorId   string `json:"creator_id" sql:"creator_id"`   // 创建者统一账号id
	AppScope    []byte `json:"app_scope" sql:"app_scope"`     // 经营范围
	appScope    *AppScope
	CreatedAt   time.Time `json:"created_at" sql:"created_at"` // 创建时间
	UpdatedAt   time.Time `json:"updated_at" sql:"updated_at"` // 更新时间
	DeletedAt   int64     `json:"delete_at" sql:"deleted_at"`  // 删除时间
}

// NewBrand 新建品牌
func NewBrand(name, logo, desc, accountId string, bizType int8) *Brand {
	return &Brand{
		Name:        name,
		Logo:        logo,
		Description: desc,
		BizType:     bizType,
		Status:      BrandStatusOK,
		AppScope:    []byte("{}"),
		CreatorId:   accountId,
		CreatedAt:   util.GetNow(),
		UpdatedAt:   util.GetNow(),
		DeletedAt:   0,
	}
}

// IsValid 是否有效
func (b *Brand) IsValid() bool {
	if b.DeletedAt != 0 {
		return false
	}
	return b.Status == BrandStatusOK
}

// SetBizType 设置经营模式
func (b *Brand) SetBizType(biz string) error {
	switch biz {
	case BrandBizTypeSelfRunStr:
		b.BizType = BrandBizTypeSelfRun
	case BrandBizTypePlatformStr:
		b.BizType = BrandBizTypePlatform
	case BrandBizTypeAgentStr:
		b.BizType = BrandBizTypeAgent
	default:
		return InvalidType
	}
	b.UpdatedAt = util.GetNow()
	return nil
}

// GetBizType 获取经营模式
func (b *Brand) GetBizType() string {
	switch b.BizType {
	case BrandBizTypeSelfRun:
		return BrandBizTypeSelfRunStr
	case BrandBizTypePlatform:
		return BrandBizTypePlatformStr
	case BrandBizTypeAgent:
		return BrandBizTypeAgentStr
	default:
		return ""
	}
}

// Delete 删除品牌
func (b *Brand) Delete() {
	b.DeletedAt = util.GetCurrentTimestamp()
}

// Block 停用
func (b *Brand) Block() {
	b.Status = BrandStatusBlocked
	b.UpdatedAt = util.GetNow()
}

// UpdateInfo 更新品牌信息
func (b *Brand) UpdateInfo(name, logo, desc string) {
	if name != "" {
		b.Name = name
	}
	if logo != "" {
		b.Logo = logo
	}
	if desc != "" {
		b.Description = desc
	}
	b.UpdatedAt = util.GetNow()
}

// AssignSuperAdmin 设置超级管理员
func (b *Brand) AssignSuperAdmin(empId int64) {
	b.SuEmpId = empId
	b.UpdatedAt = util.GetNow()
}

// GetBusinessScope 获取营业范围
func (b *Brand) GetBusinessScope() *AppScope {
	if b.appScope != nil {
		return b.appScope
	}
	if b.AppScope == nil {
		return nil
	}
	r := &AppScope{}
	_ = json.Unmarshal(b.AppScope, r)
	b.appScope = r
	return r
}

// 设置营业范围
func (b *Brand) setAppScope(bus string, op bool, empId int64) {
	s := b.GetBusinessScope()
	if s == nil {
		return
	}
	now := util.GetNow()
	switch bus {
	case "mall":
		s.Mall = op
		s.MallOpTime = now
		s.MallOpEmp = empId
	case "bg":
		s.Bg = op
		s.BgOpTime = now
		s.BgOpEmp = empId
	default:
		return
	}
	b.AppScope, _ = json.Marshal(s)
	b.UpdatedAt = now
	b.appScope = s
}

// OpenBg 开启星团app
func (b *Brand) OpenBg(empId int64) {
	b.setAppScope("bg", true, empId)
}

// OpenMall 开启商城
func (b *Brand) OpenMall(empId int64) {
	b.setAppScope("mall", true, empId)
}

// CloseBg 关闭星团app
func (b *Brand) CloseBg(empId int64) {
	b.setAppScope("bg", false, empId)
}

// CloseMall 关闭商城
func (b *Brand) CloseMall(empId int64) {
	b.setAppScope("mall", false, empId)
}

// HasBg 是否开启星团
func (b *Brand) HasBg() bool {
	s := b.GetBusinessScope()
	return s.Bg
}

// HasMall 是否开启商城
func (b *Brand) HasMall() bool {
	s := b.GetBusinessScope()
	return s.Mall
}
